<?php
/*
Legge il calendario e lo trasforma in HTML
Preleva i prossimi incontri nell'arco di 28gg

*/
ini_set("include_path", ".:/usr/lib/php:/usr/local/lib/php:/home/pischell/php/ZendFramework-1.5.1/library:/home/pischell/php/ZendGdata-1.5.0/library");
// ZEND Framework
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');

// autenticazione
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$user = "user@gmail.com";
$pass = "secret";

// traduzioni 
$weekdays = array('DOM', 'LUN', 'MAR', 'MER', 'GIO', 'VEN', 'SAB');
$months = array( 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto',  'Settembre', 'Ottobre', 'Novembre', 'Dicembre' );

try {
	$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
	$service = new Zend_Gdata_Calendar($client);

	$today = date(DATE_ATOM);
	$maxdate = mktime(0, 0, 0, date("m")+1,   date("d")-1,   date("Y"));

	$startmax = "Starts: " . date(DATE_ATOM,$maxdate);

	$query = $service->newEventQuery();
	$query->setUser('default');
	$query->setVisibility('private');
	$query->setProjection('full');
	$query->setOrderby('starttime');
	$query->setSingleevents('true');
	$query->setSortorder('ascending');
	$query->setFutureevents('true');
	$query->setStartMin($today);
	$query->setStartMax(date(DATE_ATOM,$maxdate));

	// Retrieve the event list from the calendar server
    $eventFeed = $service->getCalendarEventFeed($query);
	$date_start = '';
	$event_id = 0;
	foreach ($eventFeed as $event) {
		// debug: echo var_dump($event);
		if (ereg ("Starts: ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})T([0-9]{1,2}):([0-9]{1,2})", $event->when[0], $startdate) && $event->when[0] < $startmax)
		{
			$currdate = $startdate[1] ."-".$startdate[2] ."-".$startdate[3];
			if ($date_start != $currdate){
				if ($date_start != ''){
					echo "<div class=\"clearfix clear\"></div>\n";
				}
				echo "<div class=\"date\">\n";
				echo "<span class=\"wd\">" . $weekdays[date("w", mktime(0, 0, 0, $startdate[2], $startdate[3], $startdate[1]))] ."</span><span class=\"dn\">" . $startdate[3] . "</span>\n";
				echo "</div>\n";
				$date_start = $currdate;
			}
			ereg ("Ends: ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})T([0-9]{1,2}):([0-9]{1,2})", $event->when[0], $enddate);
		    echo "<div class=\"vevent\"><span class=\"dtstart\">" . $startdate[4] .":".$startdate[5] . "</span>\n";
			echo "<p class=\"summary\"><a href=\"#\" onclick=\"$('#info-".$event_id."').slideDown();\">" . $event->title . "</a></p></div>\n";
			echo "<div id=\"info-".$event_id."\" class=\"eventdetail\" style=\"display:none;\">\n";
			echo "<p><strong>Descrizione:</strong>".$event->content."</p>\n";
			echo "<div class=\"span-2\"><strong>Ora inizio:</strong> <span>".$startdate[4] .":".$startdate[5]."</span></div>\n";
			echo "<div class=\"span-2\"><strong>Ora fine:</strong> <span class=\"dtend\">".$enddate[4].":".$enddate[5]."</span></div>\n";
			echo "	<div class=\"clearfix\"></div>\n";
			if($event->where[0]!='') 
				echo "<div class=\"span-5\"><strong>Dove:</strong> <a href=\"http://maps.google.it/maps?q=".urlencode($event->where[0])."&amp;hl=it\" target=\"_blank\">".$event->where[0]."</a></div>\n";
			echo "	<div class=\"clearfix\"></div>\n";
			echo "<div class=\"hideme\"><a href=\"#\" onclick=\"$('#info-".$event_id."').slideUp();\">Nascondi &#x2191;</a></div>\n";
			echo "</div>\n";
			$event_id = $event_id + 1;
		}
	}
} catch (Zend_Gdata_App_Exception $e) {
    echo "Error: " . $e->getResponse();
}
?>
